<template>
  <view-box ref="viewBox" body-padding-bottom="93px">
    <empty tips="购物车空如也" icon="icon-daipingjia" v-if="empty"></empty>
    <swipeout>
      <swipeout-item v-for="(item, index) in filterList" :key="index">
        <div slot="right-menu">
          <swipeout-button @click.native="destory(index)" type="warn">删除</swipeout-button>
        </div>
        <div slot="content" class="row-box vux-1px-b">
          <div class="check-box">
            <check-icon :value.sync="item.checked"></check-icon>
          </div>
          <div class="p-img">
            <img :src="item.goods.thumb_img" alt="">
          </div>
          <div class="p-info">
            <div>
              <div class="p-name">
                {{item.goods.name}}
              </div>
              <div class="p-spec" v-if="item.spec">
                {{item.spec.value}}
              </div>
            </div>
            <div class="p-action">
              <div>
                <span class="p-shop-price">￥{{item.price}}</span>
              </div>
              <div class="p-number">
                <inline-x-number @click.native="changeQuantity(item.quantity, item)" :min="1" :max="item.stock" :value="item.quantity" v-model="item.quantity" width="30px"></inline-x-number>
              </div>
            </div>
          </div>
        </div>
      </swipeout-item>
    </swipeout>
    <div slot="bottom" class="action-bar vux-1px-b vux-1px-t" v-if="!empty">
      <div class="check-all">
        <check-icon :value.sync="checkedAll" @click.native="checkAll()">全选</check-icon>
      </div>
      <div class="total-price">合计：￥{{totalPrice}}</div>
      <div class="to-pay" @click="checkout()">去结算（{{productNumber}}）</div>
    </div>
    <footer-bar slot="bottom" selected="carts"></footer-bar>
  </view-box>
</template>
<script>
  import { CheckIcon, InlineXNumber, Swipeout, SwipeoutItem, SwipeoutButton, ViewBox, LoadMore } from 'vux'
  import FooterBar from '../components/FooterBar'
  import { CHECK_ALL_CART } from '@/store/mutation-types'
  import Empty from '@/views/components/Empty'
  export default {
    data () {
      return {
        checkedAll: false
      }
    },
    name: 'carts',
    components: {
      CheckIcon,
      InlineXNumber,
      Swipeout,
      SwipeoutItem,
      SwipeoutButton,
      ViewBox,
      FooterBar,
      LoadMore,
      Empty
    },
    computed: {
      filterList () {
        return this.$store.getters.initCartList
      },
      empty () {
        return this.$store.state.cart.loaded && this.filterList.length === 0
      },
      totalPrice () {
        return this.$store.getters.cartCheckedTotalPrice
      },
      productNumber () {
        return this.$store.getters.cartCheckedNumber
      }
    },
    mounted () {
      if (!this.$store.state.cart.loaded) {
        this.$store.dispatch('getCartList')
      }
    },
    methods: {
      destory (index) {
        this.$store.dispatch('deleteCart', index)
      },
      checkAll () {
        this.$store.commit(CHECK_ALL_CART, this.checkedAll)
      },
      changeQuantity (number, item) {
        setTimeout(() => {
          if (number !== item.quantity) {
            this.$store.dispatch('updateCart', {id: item.id, quantity: item.quantity})
          }
        }, 50)
      },
      checkout () {
        if (this.productNumber <= 0) {
          this.$vux.toast.show({
            text: '请选择商品',
            type: 'cancel'
          })
          return false
        }
        this.$router.push('/checkout')
      }
    }
  }
</script>
<style scoped lang='less'>
  .container {
    padding-bottom: 39px;
  }
  .row-box {
    display: flex;
    padding-right: 10px;
    justify-content:space-between;
    height: 100px;
    padding-bottom: 10px;
    padding-top: 10px;
  }
  .p-img {
    flex-shrink: 0;
    width: 100px;
    padding-right: 10px;
  }
  .p-img img {
    width: 100px;
    height: 100px;
  }
  .row-box .p-info {
    flex-grow: 1;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
  }
  .row-box .p-info .p-name {
    font-size: 14px;
    overflow : hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    padding-bottom: 5px;
    color: #151516;
  }
  .row-box .p-info .p-action {
    color: @tabbar-text-active-color;
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
  }
  .row-box .p-info .p-action .weui-btn {
    margin-right: 0;
  }
  .row-box  .p-info .p-action .p-shop-price {
    font-size: 16px;
    font-weight: 500;
    line-height: 16px;
    height: 16px;
  }
  .row-box  .p-info .p-action .p-market-price {
    font-size: 12px;
    height: 12px;
    line-height: 12px;
    color: #9c9c9c;
  }
  .p-spec {
    font-size: 12px;
    color: #9c9c9c;
  }
  .check-box {
    line-height: 100px;
    padding: 0 5px;
  }
  .action-bar {
    background-color: #fff;
    display: flex;
    justify-content: space-between;
    position: fixed;
    z-index: 500;
    bottom: 50px;
    width: 100%;
    height: 45px;
    line-height: 45px;
  }
  .check-all {
    padding-left: 5px;
  }
  .to-pay {
    background-color: @tabbar-text-active-color;
    padding: 0 10px;
    color: #fff;
  }
</style>